local M = {}

local math_min = math.min
local math_max = math.max
--[[
	width
	height
	dragSides = {0, 0, 0, 1} -- 上下左右
]]
function M.drawDragBox(params)
	local width = params.width or 100
	local height = params.height or 100

	local minWidth = params.minWidth or width
	local maxWidth = params.maxWidth or W

	local minHeight = params.minHeight or height
	local maxHeight = params.maxHeight or H

	local dragSides = params.dragSides or {0, 0, 0, 1}

	local group = display.newGroup()

	local function isSideDraggale(sideKey)
		if sideKey == "up" then
			return dragSides[1] == 1
		end

		if sideKey == "bottom" then
			return dragSides[2] == 1
		end

		if sideKey == "left" then
			return dragSides[3] == 1
		end

		if sideKey == "right" then
			return dragSides[4] == 1
		end
	end

	local function createDragIcon()
		local width = 6
		local tVerties = {
			0, 		-1,
			width, 	-1,

			width, 	-6,
			width + 5, 0,
			width, 	6,

			width, 1,
			-width, 1,

			-width, 6,
			-width - 5, 0,
			-width, -6,

			-width, -1,
		}

		if isSideDraggale("up") or isSideDraggale("down") then
			tVerties = {
				-1, 0,
				-1, -width,
		
				-6, -width,
				0,  -width - 5,
				6, -width,

				1, -width,
				1, width,

				6, width,
				0, width + 5,
				-6, width,

				-1, width
			}
		end

		local polygon = display.newPolygon( 100, 100, tVerties )

		return polygon
	end

	local function createRect()
		local rect = display.newRect(0, 0, width, height)

		rect:setFillColor(unpack(colorCfg[3]))
		group:insert(rect)

		local function setAnchor()
			rect.anchorX = 0
			rect.anchorY = 0
			if isSideDraggale("right") then
				rect.anchorX = 0
			elseif isSideDraggale("left") then
				rect.anchorX = 1
			end

			if isSideDraggale("down") then
				rect.anchorY = 0
			elseif isSideDraggale("up") then
				rect.anchorY = 1
			end
		end

		setAnchor()

		return rect
	end

	local rect = createRect()
	local anchorXH, anchorYH = (-rect.anchorX + 0.5), (-rect.anchorY + 0.5)
	local anchorX, anchorY = rect.anchorX, rect.anchorY
	local function createBorderRect(rect)

		local touchWidth = 2.5
		local upRect = display.newRect(group, 0, 0, width, touchWidth)
		upRect.isHorDrag = false
		upRect.isVertDrag = true
		upRect.isReverse = true

		local bottomRect = display.newRect(group, 0, 0, width, touchWidth)
		bottomRect.isHorDrag = false
		bottomRect.isVertDrag = true

		local leftRect = display.newRect(group, 0, 0, touchWidth, height)
		leftRect.isHorDrag = true
		leftRect.isVertDrag = false
		leftRect.isReverse = true

		local rightRect = display.newRect(group, 0, 0, touchWidth, height)
		rightRect.isHorDrag = true
		rightRect.isVertDrag = false

		upRect:setFillColor(unpack(colorCfg[4]))
		bottomRect:setFillColor(unpack(colorCfg[4]))
		leftRect:setFillColor(unpack(colorCfg[4]))
		rightRect:setFillColor(unpack(colorCfg[4]))


		-- isVertDrag 纵向
		local tDragRect = {
			up = {rect = upRect},
			bottom = {rect = bottomRect},
			left = {rect = leftRect,},
			right = {rect = rightRect},
		}

		local dragIcon = nil

		local function resetTransform(isHorDrag, newWidth, newHeight)
			upRect.width = newWidth
			uiUtils.setXY(upRect, newWidth*anchorXH , touchWidth/2 - anchorY*newHeight)

			bottomRect.width = newWidth
			uiUtils.setXY(bottomRect, newWidth*anchorXH, newHeight - touchWidth/2 - anchorY*newHeight)

			leftRect.height = newHeight
			uiUtils.setXY(leftRect, touchWidth/2 - anchorX*newWidth, newHeight*anchorYH)

			rightRect.height = newHeight
			uiUtils.setXY(rightRect, newWidth - touchWidth/2 - anchorX*newWidth, newHeight*anchorYH)
		end

		resetTransform(nil, rect.width, rect.height)

		local function onMove(event)
			if event.target.isHorDrag then
				local newWidth = event.frameOffsetX*(event.target.isReverse and -1 or 1) + rect.width
				newWidth = math_max(math_min(newWidth, maxWidth), minWidth)
				resetTransform(true, newWidth, rect.height)
				rect.width = newWidth

				if objUtilsE.isExist(dragIcon) then
					uiUtils.setXY(dragIcon, event.target.x, event.y - group.y)
				end
			else
				local newHeight = event.frameOffsetY*(event.target.isReverse and -1 or 1) + rect.height
				newHeight = math_max(math_min(newHeight, maxHeight), minHeight)
				resetTransform(false, rect.width, newHeight)
				rect.height = newHeight

				if objUtilsE.isExist(dragIcon) then
					uiUtils.setXY(dragIcon, event.x - group.x, event.target.y)
				end
			end
		end

		local function onCancel(event)
			objUtilsE.removeObj(dragIcon)
			dragIcon = nil
		end

		local function onBegin(event)
			if not objUtilsE.isExist(dragIcon) then
				dragIcon = createDragIcon()

				uiUtils.setXY(dragIcon, event.x - group.x, event.y - group.y)
				group:insert(dragIcon)
			end
		end

		local function onHover(event)
			if not objUtilsE.isExist(dragIcon) then
				dragIcon = createDragIcon()

				uiUtils.setXY(dragIcon, event.x - group.x, event.y - group.y)
				group:insert(dragIcon)
			end
		end


		for k, v in pairs(tDragRect) do
			if isSideDraggale(k) then
				inputUtils.mouseDelegate({
					target = v.rect,
					onMove = onMove,
					onBegin = onBegin,
					onCancel = onCancel,
					--tOffsetBounce = {xMin = 0.5},
					onHover = onHover,
					onCancelHover = onCancel
				})
			end
		end
	end

	createBorderRect(rect)

	return group
end

return M